package com.action;

import com.controller.ClusterAnalysis;
import com.controller.GetServiceCallList;
import com.controller.Predict;
import com.controller.Similarity;
import com.orm.ServiceCalledEntity;
import com.orm.WebServiceEntity;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lirance on 16/5/11.
 */
public class qosAction extends BaseAction {
    int uid1;
    int uid2;
    int k;
    double lmd;
    int uid;
    int sid;
    int cluNum;

    double similarity;
    List<WebServiceEntity> wslist;

    public int getUid1() {
        return uid1;
    }

    public void setUid1(int uid1) {
        this.uid1 = uid1;
    }

    public int getUid2() {
        return uid2;
    }

    public void setUid2(int uid2) {
        this.uid2 = uid2;
    }

    public int getK() {
        return k;
    }

    public void setK(int k) {
        this.k = k;
    }

    public double getLmd() {
        return lmd;
    }

    public void setLmd(double lmd) {
        this.lmd = lmd;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public int getCluNum() {
        return cluNum;
    }

    public void setCluNum(int cluNum) {
        this.cluNum = cluNum;
    }

    public double getSimilarity() {
        return similarity;
    }

    public void setSimilarity(double similarity) {
        this.similarity = similarity;
    }

    public List<WebServiceEntity> getWslist() {
        return wslist;
    }

    public void setWslist(List<WebServiceEntity> wslist) {
        this.wslist = wslist;
    }

    @Override
    public String execute() throws Exception {
        DecimalFormat df = new DecimalFormat("######0.0000000");
        this.setSimilarity(new Similarity().getSimilarity(uid1, uid2, 30));
        df.format(similarity);  //保留7位小数


        if (uid != 0) {
            List<ServiceCalledEntity> l = new GetServiceCallList().GetServiceCallListsByuId(uid); //用户调用的所有服务列表
            List<Integer> sl = new ArrayList<>(); //用户调用的所有服务
            for (int i = 0; i < l.size(); i++) {
                if (!sl.contains(l.get(i).getWsId())) {
                    sl.add(l.get(i).getWsId());
                }
            }
            wslist = new ArrayList<>();
            if (cluNum != 0) {
                new ClusterAnalysis().insert(cluNum);
            }

            /*int num=sl.size();
            if(num>5) num=5;
            for (int i = 1; i <= num; i++) {
                try {
                    WebServiceEntity ws = new WebServiceEntity();
                    if (cluNum == 0) {  //获取预测数据
                        ws.setClusteNum(new Predict().getPredicts(uid, sl.get(i), lmd, k, 30) + "");


                    } else {

                        ws.setClusteNum(new Predict().getPredicts_Cluster(uid, sl.get(i), lmd, k, 30) + "");
                    }
                    ws.setWsId(sl.get(i));
                    wslist.add(ws);
                } catch (Exception ignored) {
                }
            }*/
           // this.setSid(2);
            try {
                WebServiceEntity ws = new WebServiceEntity();
                if (cluNum == 0) {  //获取预测数据
                    ws.setClusteNum(new Predict().getPredicts(uid, sid, lmd, k, 30) + "");


                } else {

                    ws.setClusteNum(new Predict().getPredicts_Cluster(uid, sid, lmd, k, 30) + "");
                }
                ws.setWsId(sid);
                wslist.add(ws);
            } catch (Exception ignored) {
            }
        }


        return SUCCESS;
    }
}
